<?php
if (!defined('BASEPATH'))
	exit('No direct script access allowed');

include 'securities.php';

class Manage_tags extends Securities {

	function __construct() {
		parent::__construct('manage_tags');
		$this -> form_validation -> set_error_delimiters('<span class="error">', '</span>');
	}

	public function index() {
		$data['title'] = $this->lang->line('title_list_tags');
		$data['controller_name'] = strtolower(get_class());
		$data['manage_datatable'] = get_tags_manage_table($this -> mod_globals -> select_all(PREFIX_TABLE . 'tags'), $this -> uri -> segment(1));
		$this -> load -> view(B_MASTER . 'index', $data);

	}

	public function insert_tag() {
		$module_id = $this -> uri -> segment(1);
		if (!$this -> is_has_modify_permission($module_id)) {
			redirect('ctm-common/no-allow-modify/' . $module_id);
		}

		$data['title'] = $this->lang->line('title_new_tag');

		$this -> form_validation -> set_rules('tag_name', 'lang:form_label_tag_name', 'trim|required');

		if ($this -> form_validation -> run() === FALSE) {
			$this -> load -> view(B_MASTER . 'index', $data);
		} else {
			$tag_name = $this -> input -> post('tag_name');
			$is_saved = $this -> mod_globals -> insert(PREFIX_TABLE . 'tags', array('name' => $tag_name, 'status' => 1, 'created_date' => null));
			if ($is_saved) {
				$this -> session -> set_flashdata('success', 'Tag  was created success.');
				redirect('manage-tags');
			} else {
				$this -> session -> set_flashdata('error', 'Tag  was created fail.');
				redirect('manage-tags');
			}
		}

	}

	public function update_tag() {
		$module_id = $this -> uri -> segment(1);
		if (!$this -> is_has_modify_permission($module_id)) {
			redirect('ctm-common/no-allow-modify/' . $module_id);
		}

		$tag_id = $this -> uri -> segment(3);
		$data['title'] = $this->lang->line('title_update_tag');
		$this -> form_validation -> set_rules('tag_name', 'lang:form_label_tag_name', 'trim|required');

		if ($this -> form_validation -> run() === FALSE) {
			$data['tag_update'] = $this -> mod_globals -> select_where(PREFIX_TABLE . "tags", array('tag_id' => $tag_id));
			$this -> load -> view(B_MASTER . 'index', $data);
		} else {
			$tag_name = $this -> input -> post('tag_name');
			$is_saved = $this -> mod_globals -> update(PREFIX_TABLE . 'tags', array('name' => $tag_name), array('tag_id' => $tag_id));
			if ($is_saved) {
				$this -> session -> set_flashdata('success', 'Tag  was updated success.');
				redirect('manage-tags');
			} else {
				$this -> session -> set_flashdata('error', 'Tag  was updated fail.');
				redirect('manage-tags');
			}
		}
	}

	public function delete_tag() {
		$module_id = $this -> uri -> segment(1);
		if (!$this -> is_has_modify_permission($module_id)) {
			redirect('ctm-common/no-allow-modify/' . $module_id);
		}

		$tag_id = $this -> uri -> segment(3);
		$is_deleted = $this -> mod_globals -> delete(PREFIX_TABLE . "tags", array('tag_id' => $tag_id));
		if ($is_deleted) {
			$this -> session -> set_flashdata('success', 'Tag  was deleted success.');
			redirect('manage-tags');
		} else {
			$this -> session -> set_flashdata('error', 'Tag  was deleted fail.');
			redirect('manage-tags');
		}
	}

}
